package com.rritw.webtools.action.tool;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.HtmlUtils;

import com.rritw.webtools.action.IpsAction;
import com.rritw.webtools.util.StringUtil;

@Controller
@Scope("prototype")
public class Native2asciiAction extends IpsAction {

    private static final long serialVersionUID = 5454917407067249795L;

    private String            content;
    private String            action;
    private String            contentResult;

    public String index() {
        super.init();
        return SUCCESS;
    }

    public String create() {
        super.init();
        if (StringUtil.isEmpty(content)) {
            return ERROR;
        }
        if ("escape".equals(action)) {
            contentResult = StringEscapeUtils.escapeJava(content);
        } else if ("unescape".equals(action)) {
            contentResult = StringEscapeUtils.unescapeJava(content);
        } else {
            return ERROR;
        }
        if (!StringUtil.isEmpty(content)) {
            content = HtmlUtils.htmlEscape(content);
        }
        if (!StringUtil.isEmpty(contentResult)) {
            contentResult = HtmlUtils.htmlEscape(contentResult);
        }
        return SUCCESS;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getContentResult() {
        return contentResult;
    }

}
